﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ScytheEngine
{
	public abstract class UIScreen : GameState
	{
		#region Fields

		protected Grid m_ScreenElements = new Grid();
		protected UIScreenElement m_SelectedScreenElement;

		#endregion

		#region Handle Input

		/// <summary>
		/// Responds to user input, changing the selected entry and accepting
		/// or canceling the menu.
		/// </summary>
		public override void HandleInput( InputState input )
		{
			// Move to the previous menu entry?
			if ( input.MenuUp )
			{
				if ( m_SelectedScreenElement.Up != null )
					m_SelectedScreenElement = (UIScreenElement)m_SelectedScreenElement.Up;
			}

			// Move to the next menu entry?
			if ( input.MenuDown )
			{
				if ( m_SelectedScreenElement.Down != null )
					m_SelectedScreenElement = (UIScreenElement)m_SelectedScreenElement.Down;
			}

			// Accept or cancel the menu?
			if ( input.MenuSelect )
			{
				OnSelectEntry( m_SelectedScreenElement );
			}
			else if ( input.MenuCancel )
			{
				OnCancel();
			}
		}


		/// <summary>
		/// Handler for when the user has chosen a menu entry.
		/// </summary>
		protected virtual void OnSelectEntry( UIScreenElement element_index )
		{
			((UIScreenElement)m_ScreenElements[ m_SelectedScreenElement ]).OnSelectEntry();
		}


		/// <summary>
		/// Handler for when the user has cancelled the menu.
		/// </summary>
		protected virtual void OnCancel()
		{
			ExitScreen();
		}


		/// <summary>
		/// Helper overload makes it easy to use OnCancel as a MenuEntry event handler.
		/// </summary>
		protected void OnCancel( object sender, EventArgs e )
		{
			OnCancel();
		}


		#endregion
	}
}
